package com.tanovo.service.impl;

import com.tanovo.bo.DepartmentUserBo;
import com.tanovo.bo.DepartmentUserQuery;
import com.tanovo.config.UserInfoContext;
import com.tanovo.domain.*;
import com.tanovo.mapper.DepartmentMapper;
import com.tanovo.mapper.DepartmentUserMapper;
import com.tanovo.service.IDepartmentService;
import com.tanovo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private DepartmentUserMapper departmentUserMapper;

    @Autowired
    private IUserService iUserService;

    @Override
    public void addDepartment(Department department) {
        department.setDelFlag(0);
        department.setCreateTime(new Date());
        department.setUpdateTime(new Date());
        departmentMapper.insert(department);
    }


    @Override
    public void editDepartment(Department department) {
        department.setUpdateTime(new Date());

        departmentMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public List<Department> listbyParentId(Integer parentId) {
        DepartmentExample example = new DepartmentExample();
        example.createCriteria()
                .andParentIdEqualTo(parentId)
                .andDelFlagEqualTo(0)
                .andClientIdEqualTo(UserInfoContext.getUser().getClientId())
                .andCompanyIdEqualTo(UserInfoContext.getUser().getCompanyId());
        List<Department> departmentList = departmentMapper.selectByExample(example);
        for (Department de : departmentList) {
            Integer count = departmentMapper.hasChildren(de.getId());
            if (count != null && count > 0) {
                de.setHasChildren(true);
            }
        }
        return departmentList;
    }

    @Override
    public List<UserBaseInfo> userList(DepartmentUserQuery departmentUserQuery) {

        List<UserBaseInfo> userList= departmentUserMapper.userList(departmentUserQuery);
        return userList;
    }

    @Override
    @Transactional
    public void bindDepartmentUser(DepartmentUserBo departmentUserBo) {
        List<Integer> addUserIdList = departmentUserBo.getAddUserIdList();
        for (Integer userId : addUserIdList) {
            DepartmentUser departmentUser = new DepartmentUser();
            departmentUser.setDepartmentId(departmentUserBo.getDepartmentId());
            departmentUser.setUserId(userId);
            departmentUser.setCompanyId(UserInfoContext.getUser().getCompanyId());
            departmentUser.setClientId(UserInfoContext.getUser().getClientId());
            departmentUser.setDelFlag(0);
            departmentUser.setCreateTime(new Date());
            departmentUser.setUpdateTime(new Date());
            departmentUser.setUpdateBy(departmentUserBo.getUpdateBy());
            departmentUser.setCreateBy(departmentUserBo.getCreateBy());
            departmentUserMapper.insert(departmentUser);
        }
        this.delDepartmentUser(departmentUserBo);
    }

    @Override
    public void delDepartmentUser(DepartmentUserBo departmentUserBo) {
        List<Integer> deleteUserIdList = departmentUserBo.getDeleteUserIdList();
        for (Integer userId : deleteUserIdList) {
            DepartmentUserExample example = new DepartmentUserExample();
            example.createCriteria()
                    .andCompanyIdEqualTo(UserInfoContext.getUser().getCompanyId())
                    .andClientIdEqualTo(UserInfoContext.getUser().getClientId())
                    .andDepartmentIdEqualTo(departmentUserBo.getDepartmentId())
                    .andUserIdEqualTo(userId);
            departmentUserMapper.deleteByExample(example);
        }
    }

    @Override
    @Transactional
    public void delDepartment(List<Integer> departmentIdList) {
        for (Integer id : departmentIdList) {
            Department department = new Department();
            department.setId(id);
            department.setDelFlag(1);
            department.setUpdateTime(new Date());
            department.setUpdateBy(UserInfoContext.getUser().getId().toString());
            this.editDepartment(department);
        }
    }

    @Override
    public List<UserBaseInfo> userNotList(DepartmentUserQuery departmentUserQuery) {
        List<UserBaseInfo> list = departmentUserMapper.userNotList(departmentUserQuery);
        return list;
    }
}
